package org.dromara.netbar.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 被控端接口调用工具类
 * @author ZhouWenTao
 * @create 2025-08-06 20:49
 */
@Slf4j
public class BkdUtils {
    private static String bkdUrl;

    public static void setBkdUrl(String bkdUrl) {
        BkdUtils.bkdUrl = bkdUrl;
    }

    public static void command(Object commandBody) {
        try {
            HttpUtil.createRequest(Method.POST,  bkdUrl +"/java/command").body(JsonUtils.toJsonString(commandBody)).executeAsync();
        }catch (Exception e){
            log.error("调用Socket发送命令[/java/command]失败", e);
        }
    }

    /**
     * 给MAC地址的电脑关机
     * @param macAddress MAC地址
     */
    public static void shutdown(String macAddress) {
        if(StringUtils.isBlank(macAddress)) return;
        Map<String,String> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
//        javaCommandBody.put("command", "LOCKSCREEN");
        javaCommandBody.put("command", "SHUTDOWN");
        command(javaCommandBody);
    }

    /**
     * 给MAC地址的电脑锁屏
     * @param macAddress MAC地址
     */
    public static void lockscreen(String macAddress) {
        if(StringUtils.isBlank(macAddress)) return;
        Map<String,String> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
        javaCommandBody.put("command", "LOCKSCREEN");
//        javaCommandBody.put("command", "SHUTDOWN");
        command(javaCommandBody);
    }

    /**
     * 给MAC地址发数据
     */
    public static void sendData(String macAddress, Map<String,Object> data) {
        Map<String,String> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
        javaCommandBody.put("command", "SET_CLIENT_DATA");
        javaCommandBody.put("data", JsonUtils.toJsonString(data));
        command(javaCommandBody);
    }

    /**
     * 给MAC地址发送余额
     */
    public static void sendBalance(String macAddress,Long baseBalance, Long awardBalance) {
        Map<String,Object> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
        javaCommandBody.put("command", "SET_CLIENT_BALANCE");
        javaCommandBody.put("baseBalance", baseBalance);
        javaCommandBody.put("awardBalance", awardBalance);
        command(javaCommandBody);
    }

    public static void sendMsg(String macAddress, String msg){
        Map<String,String> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
        javaCommandBody.put("command", "SEND_MSG");
        javaCommandBody.put("msg", msg);
        command(javaCommandBody);
    }

    public static void sendMemberInfo(String macAddress, String jsonStr) {
        Map<String,String> javaCommandBody = new HashMap<>();
        javaCommandBody.put("macAddress", macAddress);
        javaCommandBody.put("command", "SET_MEMBER_INFO");
        javaCommandBody.put("data", jsonStr);
        command(javaCommandBody);
    }
}
